package com.bj58.heatmap.db;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.bj58.dsap.api.scf.contract.IHbaseApi;
import com.bj58.dsap.api.scf.contract.Result;
import com.bj58.heatmap.util.ScfUtil;

public class HBaseTest {
	private static IHbaseApi hbase;
	private static String tablename="SearchWordCount";
	static {
//		String configPath = System.getProperty("user.dir")+ "/config/scf.config";
//		System.out.println(configPath);
//		SCFInit.init(configPath);
//		String url = "tcp://dsapapi/HbaseApi";
//		hbase = ProxyFactory.create(IHbaseApi.class, url);
		ScfUtil.initSCF();
		hbase=ScfUtil.getHbaseService();
	}
	
	
	public static void main(String[] args) {
		/**通过rowkey查询 **/
		String rowkey = "北京_教育培训_20150315";
		Result rs = hbase.queryByRowKey(tablename, rowkey);
		Map<String, Object> wordlist=rs.getScfMap();
		wordlist.remove("rowKey");
		List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(wordlist.entrySet());
		Collections.sort(infoIds, new Comparator<Map.Entry<String,Object>>() {
	        @Override
	        public int compare(Map.Entry<String,Object> firstMapEntry, 
	                   Map.Entry<String,Object> secondMapEntry) {
	             Integer t1=Integer.parseInt(secondMapEntry.getValue().toString());
	             Integer t2=Integer.parseInt(firstMapEntry.getValue().toString());
	        	return t1.compareTo(t2);
	         }
		 });
		
		    for (Integer i = 0; i < infoIds.size()&&i<50; i++) {
		    	Integer tInteger=i+1;
		    	System.out.println(tInteger.toString()+infoIds.get(i).getKey().replaceFirst("cf:", "")+":"+infoIds.get(i).getValue());
		       // System.out.println(infoIds.get(i));
		     }
	    
//		Result rs=hbase.queryByRowKey("ZhHomeHeatmapHBase", prefix+rowkey);
//		System.out.println(++count + ":" + rs.getScfMap().get("rowKey")+rs.getScfMap().get("cf:pv"));
		System.exit(0);
	}

	
//	public void getByPrefix(String prefix){
//		int count=0;
//		List<Result> list=hbase.nextPagesByKeyPrefix("ZhHomeHeatmapHBase", prefix, "", 1000);
//		while(list.size()==1000){
//			count+=1000;
//			String lastRowkey=(String) list.get(999).getScfMap().get("rowKey");
//			System.out.println(lastRowkey);
//			list=hbase.nextPagesByKeyPrefix("ZhHomeHeatmapHBase", prefix, lastRowkey, 1000);
//			}
//		System.out.println("共有"+count+"条记录！");
//		System.out.println("\n------------------通过rowkey前缀查询 ---------------------");
//	}
}
